package com.alan.service.impl;

import com.alan.constants.SystemConstants;
import com.alan.domain.ResponseResult;
import com.alan.domain.entity.Article;
import com.alan.domain.entity.Category;
import com.alan.domain.vo.CategoryVo;
import com.alan.mapper.CategoryMapper;
import com.alan.service.ArticleService;
import com.alan.service.CategoryService;
import com.alan.utils.BeanCopyUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2022-09-14 08:44:02
 */
@Service
public class CategoryServiceImpl
        extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {

    @Resource
    private ArticleService articleService;

    @Resource
    private CategoryMapper mapper;

    @Override
    public ResponseResult getCategoryList() {
        /**
         * 代码规范：单表查询
         */
        //查询文章表  状态为已发布的文章
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Article> articleList = articleService.list(queryWrapper);
        //获取文章的分类id，并且去重
        Set<Long> categoryIds = articleList.stream()
                .map(Article::getCategoryId)
                .collect(Collectors.toSet());
        //查询分类表
        List<Category> categories = listByIds(categoryIds);//调用当前service的list方法
        categories = categories.stream().
                filter(category -> SystemConstants.STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());
        //封装vo
        List<CategoryVo> categoryVoList = BeanCopyUtils.copyBeanList(categories, CategoryVo.class);



        return ResponseResult.okResult(categoryVoList);
    }
}

